const service = require("../service/ypy.ask.service");
const fs = require("fs");


class ypyAskController {
  async create(ctx, next) {
    try {
      const { content, user_id, user_name, love_num, avatar, category_id } =
        ctx.request.body;
      const result = await service.create(
        content,
        user_id,
        user_name,
        love_num,
        avatar,
        category_id
      );
      ctx.body = result;
    } catch (error) {
      console.error(error);
    }
  }

  async addComment(ctx, next) {
    try {
      const { article_id, user_id, comment_id, content, avatar, user_name } =
        ctx.request.body;
      const result = await service.addComment(
        article_id,
        user_id,
        comment_id,
        content,
        avatar,
        user_name
      );
      ctx.body = result;
    } catch (error) {
      console.error(error);
    }
  }

  async list(ctx, next) {
    try {
      let { page, size } = ctx.query;
      page = String((page - 1) * size);
      const result = await service.list(page, size);
      ctx.body = result;
    } catch (error) {
      console.error(error);
    }
  }

  async commentList(ctx, next) {
    try {
      let { article_id, page, size } = ctx.query;
      page = String((page - 1) * size);
      const result = await service.commentList(article_id, page, size);
      ctx.body = result;
    } catch (error) {
      console.error(error);
    }
  }

  async like(ctx, next) {
    try {
      let { article_id, islike, user_id } = ctx.query;
      // console.log(article_id);
      const result = await service.like(article_id, islike, user_id);
      ctx.body = result;
    } catch (error) {
      console.error(error);
    }
  }

  async commentLke(ctx, next) {
    try {
      let { comment_id, islike, article_id, user_id } = ctx.query;
      const result = await service.commentLke(
        comment_id,
        islike,
        article_id,
        user_id
      );
      ctx.body = result;
    } catch (error) {
      console.error(error);
    }
  }

  async popular(ctx, next) {
    try {
      const result = await service.popular();
      ctx.body = result;
    } catch (error) {
      console.error(error);
    }
  }
  async search(ctx, next) {
    try {
      const { keywords } = ctx.request.body;
      const result = await service.search(keywords);
      ctx.body = result;
    } catch (error) {
        console.log(error);
    }
  }
}
module.exports = new ypyAskController();
